package org.gp953.order.controller;

import org.gp953.basic.exception.BusinessException;
import org.gp953.basic.util.AjaxResult;
import org.gp953.basic.util.LoginContext;
import org.gp953.basic.util.PageList;
import org.gp953.order.domain.AdoptOrder;
import org.gp953.order.query.AdoptOrderQuery;
import org.gp953.order.service.IAdoptOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/adoptOrder")
public class AdoptOrderController {

    @Autowired
    private IAdoptOrderService adoptOrderService;

    @PostMapping("/submit")
    public AjaxResult adoptPet(@RequestBody Map<String,Object> map, HttpServletRequest request){
        try {
            String payData = adoptOrderService.adoptPet(map, LoginContext.getLoginInfo(request).getId());
            return AjaxResult.me().setResultObj(payData);
        } catch (BusinessException be) {
            be.printStackTrace();
            return AjaxResult.me().setMessage(be.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("系统繁忙，稍后重试！");
        }
    }

    //平台员工
    @PostMapping("/admin")
    public PageList<AdoptOrder> admin(@RequestBody AdoptOrderQuery query, HttpServletRequest request){
        return adoptOrderService.admin(query,LoginContext.getLoginInfo(request));
    }

    //用户
    @PostMapping("/user")
    public PageList<AdoptOrder> user(@RequestBody AdoptOrderQuery query, HttpServletRequest request){
        return adoptOrderService.user(query,LoginContext.getLoginInfo(request));
    }

    /**
     * 查询单条数据详情
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public AdoptOrder getInfo(@PathVariable("id") Long id){
        AdoptOrder adoptOrder = adoptOrderService.queryById(id);
        System.out.println(adoptOrder);
        return adoptOrder;
    }

    /**
     * 通过paysn支付账单唯一编号查询单条数据详情
     * @param paysn
     * @return
     */
    @GetMapping("/paysn/{paysn}")
    public AdoptOrder getOrderInfo(@PathVariable("paysn") String paysn){
        AdoptOrder adoptOrder = adoptOrderService.queryByUnionPaySn(paysn);

        return adoptOrder;
    }



}
